package com.wangshuo.common;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wangshuo.config.Result;
import com.wangshuo.config.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginProtectInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtHelper jwtHelper;
    @Override
    public boolean preHandle(HttpServletRequest request , HttpServletResponse response, Object handler) throws Exception{
        String token=request.getHeader("token");
        if(StringUtils.isEmpty(token)||jwtHelper.isExpiration(token)){
            Result result=Result.build(null, ResultCodeEnum.NOTLOGIN);
            ObjectMapper objectMapper=new ObjectMapper();
            String json=objectMapper.writeValueAsString(result);
            response.getWriter().print(json);
            return false;
        }else{
            return true;
        }


    }

}